﻿using System;
using System.Text;

namespace jQueryCSharp.ui
{
    public class AccordionBuilder : IjQueryCSharpUIBuilder<AccordionBuilder>
    {
        private const string TEMPLATE = "$(\"{0}{1}\").accordion({2});";
        private readonly Accordion _ui;
        private readonly jQueryCSharpUIBuilder _builder;

        /// <summary>
        /// Construtor
        /// </summary>
        /// <param name="builder">jQueryCSharpUIBuilder</param>
        public AccordionBuilder( jQueryCSharpUIBuilder builder )
            : this( builder, "" )
        {
        }

        public AccordionBuilder( jQueryCSharpUIBuilder builder, string identifier )
        {
            _ui = new Accordion( identifier );
            _builder = builder;
        }

        #region Métodos
        public AccordionBuilder ID( string value )
        {
            _ui.ID = value;
            return this;
        }

        public AccordionBuilder Class( string value )
        {
            _ui.Class = value;
            return this;
        }

        /// <summary>
        /// Disables (true) or enables (false) the accordion. Can be set when initialising (first creating) the accordion.
        /// </summary>
        /// <param name="value">bool</param>
        /// <returns></returns>
        public AccordionBuilder Disabled( bool value )
        {
            _ui.Disabled = value;
            return this;
        }

        /// <summary>
        /// Selector for the active element. Set to false to display none at start. Needs collapsible: true
        /// </summary>
        /// <param name="value">string</param>
        /// <returns></returns>
        public AccordionBuilder Active( string value )
        {
            _ui.Active = value;
            return this;
        }

        /// <summary>
        /// Selector for the active element. Set to false to display none at start. Needs collapsible: true
        /// </summary>
        /// <param name="value">int</param>
        /// <returns></returns>
        public AccordionBuilder Active( int value )
        {
            _ui.Active = value.ToString( );
            return this;
        }

        /// <summary>
        /// Choose your favorite animation, or disable them (set to false). In addition to the default, 'bounceslide' and all defined easing methods are supported ('bounceslide' requires UI Effects Core).
        /// </summary>
        /// <param name="value">string</param>
        /// <returns></returns>
        public AccordionBuilder Animated( string value )
        {
            _ui.Animated = value;
            return this;
        }

        /// <summary>
        /// If set, the highest content part is used as height reference for all other parts. Provides more consistent animations.
        /// </summary>
        /// <param name="value">bool</param>
        /// <returns></returns>
        public AccordionBuilder AutoHeight( bool value )
        {
            _ui.AutoHeight = value;
            return this;
        }

        /// <summary>
        /// If set, clears height and overflow styles after finishing animations. This enables accordions to work with dynamic content. Won't work together with autoHeight.
        /// </summary>
        /// <param name="value">bool</param>
        /// <returns></returns>
        public AccordionBuilder ClearStyle( bool value )
        {
            _ui.ClearStyle = value;
            return this;
        }

        /// <summary>
        /// Whether all the sections can be closed at once. Allows collapsing the active section by the triggering event (click is the default).
        /// </summary>
        /// <param name="value">bool</param>
        /// <returns></returns>
        public AccordionBuilder Collapsible( bool value )
        {
            _ui.Collapsible = value;
            return this;
        }

        /// <summary>
        /// The event on which to trigger the accordion.
        /// </summary>
        /// <param name="value">string</param>
        /// <returns></returns>
        public AccordionBuilder Event( string value )
        {
            _ui.Event = value;
            return this;
        }

        /// <summary>
        /// If set, the accordion completely fills the height of the parent element. Overrides autoheight.
        /// </summary>
        /// <param name="value">bool</param>
        /// <returns></returns>
        public AccordionBuilder FillSpace( bool value )
        {
            _ui.FillSpace = value;
            return this;
        }

        /// <summary>
        /// Selector for the header element.
        /// </summary>
        /// <param name="value">string</param>
        /// <returns></returns>
        public AccordionBuilder Header( string value )
        {
            _ui.Header = value;
            return this;
        }

        /// <summary>
        /// Icons to use for headers. Icons may be specified for 'header' and 'headerSelected', and we recommend using the icons native to the jQuery UI CSS Framework manipulated by jQuery "UI ThemeRoller" (http://www.themeroller.com/)
        /// </summary>
        /// <param name="value">string</param>
        /// <returns></returns>
        public AccordionBuilder Icons( string value )
        {
            _ui.Icons = value;
            return this;
        }

        /// <summary>
        /// If set, looks for the anchor that matches location.href and activates it. Great for href-based state-saving. Use navigationFilter to implement your own matcher.
        /// </summary>
        /// <param name="value">bool</param>
        /// <returns></returns>
        public AccordionBuilder Navigation( bool value )
        {
            _ui.Navigation = value;
            return this;
        }

        /// <summary>
        /// Overwrite the default location.href-matching with your own matcher.
        /// </summary>
        public string NavigationFilter { get; set; }
        #endregion

        #region Override
        public new jQueryCSharpUIBuilder ToString( )
        {
            var sb = new StringBuilder( );

            var sinal = String.IsNullOrEmpty( _ui.Class ) ? "#" : ".";
            var id = String.IsNullOrEmpty( _ui.Class ) ? _ui.ID : _ui.Class;

            sb.AppendFormat( TEMPLATE, sinal, id, _ui );

            _builder.Append( sb.ToString( ) );

            return _builder;
        }
        #endregion

    }
}
